package cn.org.eshow.webapp.action;

import cn.org.eshow.bean.query.NotificationRemindQuery;
import cn.org.eshow.model.NotificationRemind;
import cn.org.eshow.model.User;
import cn.org.eshow.service.AccessTokenManager;
import cn.org.eshow.service.NotificationRemindManager;
import cn.org.eshow.webapp.util.RenderUtil;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * 通知数量API接口
 */
@AllowedMethods({"mine", "reset"})
public class NotificationRemindAction extends ApiBaseAction<NotificationRemind> {

    private static final long serialVersionUID = 1L;

    @Autowired
    private NotificationRemindManager notificationRemindManager;
    @Autowired
    private AccessTokenManager accessTokenManager;

    private NotificationRemind notificationRemind = new NotificationRemind();
    private List<NotificationRemind> notificationReminds = new ArrayList<NotificationRemind>();
    private NotificationRemindQuery query = new NotificationRemindQuery();

    /**
     * 我的消息提醒数量
     */
    public void mine() {
        User user = accessTokenManager.isValid(accessToken);
        if (user == null) {
            RenderUtil.expires();//用户信息过期
            return;
        }
        query.setUserId(user.getId());
        notificationRemind = notificationRemindManager.check(query);
        RenderUtil.success("获取成功", notificationRemind == null ? new NotificationRemind() : notificationRemind);
    }

    /**
     * 清零我的提醒数量
     */
    public void reset() throws Exception {
        if (query.getField() == null) {
            RenderUtil.failure("清空字段不能为空");
            return;
        }
        User user = accessTokenManager.isValid(accessToken);
        if (user == null) {
            RenderUtil.expires();//用户信息过期
            return;
        }
        if (query != null) {
            notificationRemindManager.reset(user.getId(), query.getField());
        }
        RenderUtil.success("消息提醒已清空");
    }

    public NotificationRemind getNotificationRemind() {
        return notificationRemind;
    }

    public void setNotificationRemind(NotificationRemind notificationRemind) {
        this.notificationRemind = notificationRemind;
    }

    public List<NotificationRemind> getNotificationReminds() {
        return notificationReminds;
    }

    public void setNotificationReminds(List<NotificationRemind> notificationReminds) {
        this.notificationReminds = notificationReminds;
    }

    public NotificationRemindQuery getQuery() {
        return query;
    }

    public void setQuery(NotificationRemindQuery query) {
        this.query = query;
    }

}